<?php

namespace App\Http\Controllers\Admin\SystemManage;

use App\Enum\ZeroOneEnum;
use App\Http\Requests\PermissionCreateRequest;
use App\Http\Requests\PermissionUpdateRequest;
use App\Response\AdminRep;
use App\Services\AdminService\Models\AdminPermissionsModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\AdminService\Models\AdminPermissionsModel as Permission;

class ActionController extends Controller
{
    use AdminRep;

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $menus = AdminPermissionsModel::with([
            'childs' => function ($query) {
                $query->with(['icon', 'childs' => function ($query) {
                    $query->with(['icon', 'childs' => function ($query) {
                        $query->with("icon");
                    }]);
                }]);
            }
            , 'icon'])->where('parent_id', 0)->orderBy('sort', 'desc')->get()->toArray();


        $permissions = $this->tree(AdminPermissionsModel::where('is_page', ZeroOneEnum::ONE)->get()->toArray());
        return view('admin.system_manage.action.index', compact('menus', 'permissions'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $permissions = $this->tree();
        return view('admin.permission.create', compact('permissions'));
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(PermissionCreateRequest $request)
    {
        $data = $request->all();
        $data['is_page'] = ZeroOneEnum::ZERO;
        if (Permission::create($data)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $permission = Permission::findOrFail($id);
        $permissions = $this->tree();
        return view('admin.permission.edit', compact('permission', 'permissions'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PermissionUpdateRequest $request)
    {
        $permission = Permission::findOrFail($request->get('id'));
        $data = $request->all();
        if ($permission->update($data)) {
            return $this->responseSucc();
        }
        return $this->responseFail('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $id = $request->get('id');
        if (empty($id)) {
            return $this->responseFail('请选择删除项');
        }
        $permission = Permission::find($id);
        if (!$permission) {
            return $this->responseFail('权限不存在');
        }
        //如果有子权限，则禁止删除
        if (Permission::where('parent_id', $id)->first()) {
            return $this->responseFail('存在子权限禁止删除');
        }
        if ($permission->delete()) {
            return $this->responseSucc();
        }
        return $this->responseFail('删除失败');
    }
}
